package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理注册");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty() ||
                nickname == null || nickname.isEmpty() ||
                ageStr == null || ageStr.isEmpty() || !ageStr.matches("[0-9]+")
        ) {
            try {
                System.out.println(username + "," + password + "," + nickname + "," + ageStr);
                response.sendRedirect("/reg_info_error.html");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);

        int age = Integer.parseInt(ageStr);
        try (
                Connection connection = DBUtil.getConnection();
        ) {
            //1、验证用户是否已经存在
            String sql = "SELECT 1 FROM userinfo WHERE username=? AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                try {
                    response.sendRedirect("/have_user.html");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //2、用户不存在,将注册信息插入到数据库中
            String sql1 = "INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1, username);
            ps1.setString(2, password);
            ps1.setString(3, nickname);
            ps1.setInt(4, age);
            int num = ps1.executeUpdate();
            if (num > 0) {
                try {
                    response.sendRedirect("/reg_success.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
//    public void login(HttpServletRequest request, HttpServletResponse response) {
    public void login(String username,String password, HttpServletResponse response) {
        System.out.println("开始处理登录");
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
        System.out.println("登录信息:"+username+","+password);
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty()
        ) {
            try {
                response.sendRedirect("/login_info_error.html");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (
                Connection connection = DBUtil.getConnection();
        ) {
            String sql = "SELECT username password FROM userinfo WHERE username=? AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                try {
                    response.sendRedirect("/login_success.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response.sendRedirect("/login_fail.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理动态页面");
        try (
                Connection connection = DBUtil.getConnection();
        ) {
            String sql = "SELECT id,username,password,nickname,age FROM userinfo";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("    <center>");
            pw.println("        <h1>用户列表</h1>");
            pw.println("        <table border=\"1\">");
            pw.println("            <tr>");
            pw.println("                <td>ID</td>");
            pw.println("                <td>用户名</td>");
            pw.println("                <td>密码</td>");
            pw.println("                <td>昵称</td>");
            pw.println("                <td>年龄</td>");
            pw.println("            </tr>");
            while (rs.next()) {
                int id = rs.getInt("id");
                pw.println("            <tr>");
                pw.println("                <td>" + id + "</td>");
                pw.println("                <td><a href='/articleList?userId=" + id + "&username=" + rs.getString("username") + "'>" + rs.getString("username") + "</a></td>");
                pw.println("                <td>" + rs.getString("password") + "</td>");
                pw.println("                <td>" + rs.getString("nickname") + "</td>");
                pw.println("                <td>" + rs.getInt("age") + "</td>");
                pw.println("            </tr>");
            }
            pw.println("        </table>");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();//结果集释放
            ps.close();//执行计划释放

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }


    }

}
